﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;
using System.IO;
using GRMS.App_Code;

namespace GRMS
{
    public static class XMLReader
    {
        /// <summary>
        /// Returns the value of a specified XML node
        /// </summary>
        /// <param name="file">Name of XML file</param>
        /// <param name="name">Name of Node</param>
        /// <returns>XmlNode object for given node name</returns>
        public static Hashtable GetValueFrom(string file, string name, Session session)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load((session.Root + file).Replace("/", @"\"));
            string[] nodes = name.Split(' ');
            string nodeSelect = "/";
            foreach (string s in nodes)
                nodeSelect += "/" + s;
            XmlNode node = doc.SelectSingleNode(nodeSelect);

            Hashtable ht = new Hashtable();
            XmlAttributeCollection attr = node.Attributes;
            for (int i = 0; i < attr.Count; i++)
                ht.Add(attr[i].Name, attr[i].Value);

            ht.Add("value", node.InnerText);

            return ht;
        }
        public static Hashtable GetValueFrom(string file, string name, string id, Session session)
        {
            Dictionary<string, Hashtable> dict = GetValuesFrom(file, name, session);

            foreach (KeyValuePair<string, Hashtable> kvp in dict)
            {
                if (kvp.Key == id)
                    return kvp.Value;
            }

            return new Hashtable();
        }

        public static Dictionary<string, Hashtable> GetValuesFrom(string file, string name, Session session)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load((session.Root + file).Replace("/", @"\"));
            string[] nodes = name.Split(' ');
            string nodeSelect = "/";
            foreach (string s in nodes)
                nodeSelect += "/" + s;
            XmlNodeList list = doc.SelectNodes(nodeSelect);

            Dictionary<string, Hashtable> dict = new Dictionary<string,Hashtable>();
            foreach (XmlNode node in list)
            {
                Hashtable ht = new Hashtable();
                if (node.Name == "check")
                {
                    Hashtable sub = new Hashtable();
                    XmlAttributeCollection attr = node.Attributes;
                    for (int i = 0; i < attr.Count; i++)
                        if (attr[i].Name == "id")
                            sub.Add(attr[i].Value, node.InnerText);
                        else
                            sub.Add(attr[i].Name, attr[i].Value);
                    if (attr.Count > 0)
                        ht.Add(attr[0].Value, sub);
                }
                else if (!node.InnerXml.Contains("CDATA"))
                {
                    foreach (XmlNode child in node.ChildNodes)
                    {
                        Hashtable sub = new Hashtable();
                        XmlAttributeCollection attr = child.Attributes;
                        for (int i = 0; i < attr.Count; i++)
                            if (attr[i].Name == "id")
                                sub.Add(attr[i].Value, child.InnerText);
                            else
                                sub.Add(attr[i].Name, attr[i].Value);
                        if (attr.Count > 0)
                            ht.Add(attr[0].Value, sub);
                    }
                }
                else 
                {
                    ht.Add("value", node.InnerText);
                }

                XmlAttributeCollection attr1 = node.Attributes;
                dict.Add(attr1[0].Value, ht);
            }

            return dict;
        }
        public static Dictionary<string, Hashtable> GetValuesFrom(string file, string name, string id, Session session)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load((session.Root + file).Replace("/", @"\"));
            string[] nodes = name.Split(' ');
            string nodeSelect = "/";
            foreach (string s in nodes)
                nodeSelect += "/" + s;
            XmlNodeList list = doc.SelectNodes(nodeSelect);

            Dictionary<string, Hashtable> dict = new Dictionary<string, Hashtable>();
            foreach (XmlNode node in list)
            {
                if (node.Attributes["id"].Value == id)
                {
                    foreach (XmlNode child in node.ChildNodes)
                    {
                        XmlAttributeCollection attr = child.Attributes;
                        Hashtable sub = new Hashtable();
                        for (int i = 0; i < attr.Count; i++)
                            if (attr[i].Name == "id")
                                sub.Add(attr[i].Value, child.InnerText);
                            else
                                sub.Add(attr[i].Name, attr[i].Value);
                        dict.Add(attr[0].Value, sub);
                    }
                    XmlAttributeCollection attr1 = node.Attributes;
                }
            }
            return dict;
        }
    }
}
